<template>
  <div ref="canvas"></div>
</template>

<script lang="ts" setup>
import * as THREE from "three";
import Stats from "three/examples/jsm/libs/stats.module";
import { GUI } from "three/examples/jsm/libs/lil-gui.module.min.js";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { ref, onMounted } from "vue";
import { addLargeGroundPlane, initDefaultLighting } from "@/book/ThreeUtils";

const canvas = ref<any>();

const scene = new THREE.Scene();

const renderer = new THREE.WebGLRenderer({ antialias : true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight);
camera.position.set(-30, 40, 30);
camera.lookAt(0, 0, 0);

const orbitControl = new OrbitControls(camera, renderer.domElement);
orbitControl.update();

const stat = Stats();

initDefaultLighting(scene, undefined);

const groundPlane = addLargeGroundPlane(scene, false);
groundPlane.position.y = 0;

const cubeGeom = new THREE.BoxGeometry(4, 4, 4);
const cubeMaterial = new THREE.MeshStandardMaterial({ color: 0xff0000 });
const cube = new THREE.Mesh(cubeGeom, cubeMaterial);
cube.castShadow = true;
cube.position.set(-10, 4, 0);
scene.add(cube);

const sphereGeom = new THREE.SphereGeometry(4, 20, 20);
const sphereMaterial = new THREE.MeshStandardMaterial({ color: 0x7777ff });
const sphere = new THREE.Mesh(sphereGeom, sphereMaterial);
sphere.castShadow = true;
sphere.position.set(20, 0, 2);
scene.add(sphere);

const cylinderGeom = new THREE.CylinderGeometry(2, 2, 20);
const cylinderMaterial = new THREE.MeshStandardMaterial({color: 0x77ff77});
const cylinder = new THREE.Mesh(cylinderGeom, cylinderMaterial);
cylinder.castShadow = true;
cylinder.position.set(0, 0, 1);
scene.add(cylinder);

const ambientLight = new THREE.AmbientLight(0x353535);
scene.add(ambientLight);

const controlObj = {
  rotationSpeed: 0.02,
  bouncingSpeed: 0.03,
  scalingSpeed: 0.03,
};

const gui = new GUI();
// @ts-ignore
gui.add(controlObj, 'rotationSpeed', 0, 0.5).step(0.1).name("正方体旋转速度");
// @ts-ignore
gui.add(controlObj, 'bouncingSpeed', 0, 0.5).step(0.1).name("球体弹跳速度");
// @ts-ignore
gui.add(controlObj, 'scalingSpeed', 0, 0.5).step(0.1).name("圆柱体变形速度");

let step = 0;
let scalingStep = 0;

const render = () => {
  stat.update();

  cube.rotation.x += controlObj.rotationSpeed;
  cube.rotation.y += controlObj.rotationSpeed;
  cube.rotation.z += controlObj.rotationSpeed;

  // bounce the sphere up and down
  step += controlObj.bouncingSpeed;
  sphere.position.x = 20 + ( 10 * (Math.cos(step)));
  sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step)));

  // scale the cylinder
  scalingStep += controlObj.scalingSpeed;
  const scaleX = Math.abs(Math.sin(scalingStep / 4));
  const scaleY = Math.abs(Math.cos(scalingStep / 5));
  const scaleZ = Math.abs(Math.sin(scalingStep / 7));
  cylinder.scale.set(scaleX, scaleY, scaleZ);

  requestAnimationFrame(render);
  renderer.render(scene, camera);
};

onMounted(() => {
  canvas.value.appendChild(stat.domElement);
  canvas.value.appendChild(renderer.domElement);

  render();
});

</script>
